package vip.liux.contracts.shared.enums.file;

import vip.liux.contracts.shared.kits.FileValidator;
import vip.liux.core.enums.IEnum;

import java.util.Set;
import java.util.regex.Pattern;

public enum FileType implements IEnum {
    IMAGE(0, "图片") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 5, Set.of(Pattern.compile("jpg|jpeg|png|gif")));
        }
    },
    VIDEO(1, "视频") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 50, Set.of(Pattern.compile("mp4|avi|rmvb")));
        }
    },
    AUDIO(2, "音频") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 10, Set.of(Pattern.compile("mp3|wav|flac")));
        }
    },
    DOCUMENT(3, "文档") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 10, Set.of(Pattern.compile("doc|docx|pdf")));
        }
    },
    ARCHIVE(4, "压缩包") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 20, Set.of(Pattern.compile("zip|rar|7z")));
        }
    },
    ANNEX(0, "附件") {
        @Override
        public FileValidator getFileValidator() {
            return new FileValidator(1024 * 1024 * 10, Set.of(Pattern.compile(".*")));
        }
    };

    final int value;

    final String desc;

    FileType(int value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    public static FileType getFileType(String extensionName) {
        if (extensionName == null) {
            return ANNEX; // 默认返回 ANNEX 或其他适当的默认值
        }
        return switch (extensionName) {
            case "doc", "docx", "pdf" -> DOCUMENT;
            case "jpg", "jpeg", "png", "gif" -> IMAGE;
            case "mp4", "avi", "rmvb" -> VIDEO;
            case "mp3", "wav", "flac" -> AUDIO;
            case "zip", "rar", "7z" -> ARCHIVE;
            default -> ANNEX;
        };
    }

    @Override
    public int getValue() {
        return value;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public abstract FileValidator getFileValidator();

}
